package com.itdr.protal.controller;


import com.itdr.common.Const;
import com.itdr.common.ServerResponse;
import com.itdr.pojo.vo.UserVO;
import com.itdr.protal.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author AirZhang
 * @since 2020-10-20
 */
@RestController
@RequestMapping("/order/")
public class OrderController {
    @Autowired
    IOrderService orderService;

    @GetMapping("insert")
    public ServerResponse insert(HttpSession httpSession, Integer shippingId, BigDecimal payment,
                                 @RequestParam(required=false,defaultValue = "1") Integer pageNum,
                                 @RequestParam(required=false,defaultValue = "5") Integer pageSize){
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }
        return orderService.insertOne(u.getId(),shippingId,payment,pageNum,pageSize);
    }

    /**
     * 订单列表
     * @param httpSession
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("list")
    public ServerResponse listAll(HttpSession httpSession,
                                 @RequestParam(required=false,defaultValue = "1") Integer pageNum,
                                 @RequestParam(required=false,defaultValue = "5") Integer pageSize,
                                 @RequestParam(required=false,defaultValue = "10") Integer status){
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }
        return orderService.listAll(u.getId(),pageNum,pageSize,status);
    }
}

